package com.reremouse.rerebbs.web.mb.mag;

import com.reremouse.model.AdvModel;
import com.reremouse.rerebbs.dao.IAdvDAO;
import com.reremouse.rerebbs.dao.IImgDAO;
import com.reremouse.service.ImageService;
import com.reremouse.util.SpringHelper;
import org.primefaces.component.fileupload.FileUpload;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.Serializable;

@ManagedBean
@ViewScoped
public class AAdv
        implements Serializable {
    AdvModel adv = new AdvModel();
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
    IAdvDAO advDAO = (IAdvDAO) SpringHelper.getSpringBean("AdvDAO");
    IImgDAO imgDAO = (IImgDAO) SpringHelper.getSpringBean("ImgDAO");
    FileUpload fu = new FileUpload();

    @PostConstruct
    public void init() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String id = request.getParameter("id");
        if (id != null) {
            this.adv = this.advDAO.findAdv(Long.parseLong(id));
        }
    }

    public AdvModel getAdv() {
        return this.adv;
    }

    public void setAdv(AdvModel adv) {
        this.adv = adv;
    }

    public FileUpload getFu() {
        return this.fu;
    }

    public void setFu(FileUpload fu) {
        this.fu = fu;
    }

    public String button1_action() {
        this.advDAO.updateAdvModel(this.adv);

        return "ListAdv?faces-redirect=true";
    }

    public void up_action(FileUploadEvent event) {
        try {
            UploadedFile item = event.getFile();
            String str = "";
            Long tl = Long.valueOf(item.getSize());


            Float k = Float.valueOf(1024.0F);
            Float m = Float.valueOf(1048576.0F);
            Float g = Float.valueOf(1.073742E+009F);
            if ((float) tl.longValue() < k.floatValue()) {
                str = tl.toString() + "B";
            } else if ((k.floatValue() <= (float) tl.longValue()) && ((float) tl.longValue() < m.floatValue())) {
                Float te = Float.valueOf(Math.round((float) tl.longValue() / k.floatValue() * 100.0F) / 100.0F);
                str = te.toString() + "KB";
            } else if (((float) tl.longValue() >= m.floatValue()) && ((float) tl.longValue() < g.floatValue())) {
                Float te = Float.valueOf(Math.round((float) tl.longValue() / m.floatValue() * 100.0F) / 100.0F);
                str = te.toString() + "MB";
            } else {
                Float te = Float.valueOf(Math.round((float) tl.longValue() / g.floatValue() * 100.0F) / 100.0F);
                str = te.toString() + "GB";
            }
            String n = item.getFileName();
            int l1 = n.lastIndexOf("\\");
            int l2 = n.lastIndexOf(".");
            String ext = "." + n.substring(l2 + 1).toLowerCase();
            String name = n.substring(l1 + 1, l2);
            long imgId = System.nanoTime();
            this.imgDAO.saveImg(item.getInputstream(), imgId);
            if ((ext.toLowerCase().equals(".jpg")) || (ext.toLowerCase().equals(".gif")) || (ext.toLowerCase().equals(".png"))) {
                ImageService s = (ImageService) SpringHelper.getSpringBean("ImageService");
                s.transferUserImagesToRelativeDir((ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext(), Long.valueOf(imgId));
            }
            System.out.println("upload executed.");
            this.fu = new FileUpload();
            String picUrl = "servlet/ShowAbstractImage?id=" + imgId;
            this.adv.setSrc(picUrl);
        } catch (Exception ee) {
            ee.printStackTrace();
        }
    }
}



/* Location:           F:\jsp源码\蝙蝠论坛系统\rerebbs\WEB-INF\classes\

 * Qualified Name:     com.reremouse.rerebbs.web.mb.mag.AAdv

 * JD-Core Version:    0.7.0.1

 */